

import java.util.HashMap;
import java.util.Map;

/* SID - state identifier
 * E - element of alphabet
 * S - state
 */
public class TransitionFunction<SID,E,S> {
	// two levels of hashmaps to emulate a table
	private Map<SID,Map<E,S>> firstLevel;
	
	public TransitionFunction() {
		firstLevel = new HashMap<SID,Map<E,S>>();
	}
	
	public void setResult(SID r, E c, S s) {
		if (!firstLevel.containsKey(r)) firstLevel.put(r, new HashMap<E,S>());
		firstLevel.get(r).put(c, s);
	}
	
	public S getResult(SID r, E c) {
		if (!firstLevel.containsKey(r) || !firstLevel.get(r).containsKey(c))
			return null;
		return firstLevel.get(r).get(c);
	}
	
	public String toString() {
		String out = "";
		for (Map.Entry<SID,Map<E,S>> kv : firstLevel.entrySet()) {
			out += kv.getKey() + " -- ";
			for (Map.Entry<E, S> kv2 : kv.getValue().entrySet()) {
				out += kv2.getKey() + ": ";
				out += kv2.getValue();
				out += ", ";
			}
			out += "\n";
		}
		return out;
	}
}
